package com.dongdongshop.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongdongshop.mapper.TbContentCategoryMapper;
import com.dongdongshop.mapper.TbContentMapper;
import com.dongdongshop.pojo.TbContent;
import com.dongdongshop.pojo.TbContentCategory;
import com.dongdongshop.pojo.TbContentCategoryExample;
import com.dongdongshop.pojo.TbContentExample;
import com.dongdongshop.pojo.vo.ContentVo;
import com.dongdongshop.service.ContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
@Component
public class ContentServiceImpl implements ContentService {
    @Resource
    private TbContentCategoryMapper contentCategoryMapper;
    @Resource
    private TbContentMapper contentMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<TbContentCategory> getContentInfo(String name) {
        //List<TbContentCategory> contentCategories = contentCategoryMapper.selectByExample(new TbContentCategoryExample());
        List<TbContentCategory> contentCategories =  contentCategoryMapper.getContentInfo(name);
        return contentCategories;
    }

    @Override
    public void insertContent(TbContentCategory contentCategory) {
        contentCategoryMapper.insertSelective(contentCategory);
    }

    @Override
    public TbContentCategory updateContentInfo(Long id) {
        TbContentCategory tbContentCategory = contentCategoryMapper.selectByPrimaryKey(id);
        return tbContentCategory;
    }

    @Override
    public void updateContent(TbContentCategory contentCategory) {
        contentCategoryMapper.updateByPrimaryKeySelective(contentCategory);
    }

    @Override
    public void deleteBatch(Integer[] ids) {
        contentCategoryMapper.deleteBatch(ids);
    }

    @Override
    public List<ContentVo> getContentInfos() {
        List<ContentVo> tbContents = contentMapper.selectByContentVo();
        return tbContents;
    }

    @Override
    public List<TbContentCategory> getContentCategory() {
        List<TbContentCategory> contentCategories = contentCategoryMapper.selectByExample(new TbContentCategoryExample());
        return contentCategories;
    }

    @Override
    public void insertContentss(TbContent content) {
        contentMapper.insertSelective(content);
        //更新缓存
        Long categoryId = content.getCategoryId();
        String key = "contents";
        String feild = "imgs:" + categoryId;
        if(redisTemplate.boundHashOps(key).hasKey(feild)){
            List<TbContent> contents = contentMapper.getContentBannerInfo(categoryId);
            redisTemplate.boundHashOps(key).put(feild,contents);
        }
    }

    @Override
    public TbContent updateContentInfos(Long id) {
        TbContent content = contentMapper.selectByPrimaryKey(id);
        return content;
    }

    @Override
    public void updateContentss(TbContent content) {
        contentMapper.updateByPrimaryKeySelective(content);
        //更新缓存
        Long categoryId = content.getCategoryId();
        String key = "contents";
        String feild = "imgs:" + categoryId;
        if(redisTemplate.boundHashOps(key).hasKey(feild)){
            List<TbContent> contents = contentMapper.getContentBannerInfo(categoryId);
            redisTemplate.boundHashOps(key).put(feild,contents);
        }
    }

    @Override
    public void deleteBatchs(Integer[] ids) {
        contentMapper.deleteBatchs(ids);
    }

    @Override
    public List<TbContent> getContentBannerInfo(Long categoryId) {
        //用hash类型将图片存入Redis
        //先查询Redis中是否有权限树，如果有，直接返回。入股没有则查询数据库，然后再把数据保存到Redis中，最后返回
        String key = "contents";
        String feild = "imgs:" + categoryId;
        //判断Redis中是否存在key
        if(redisTemplate.boundHashOps(key).hasKey(feild)){
            //如果缓存中有，则在缓存中取数据
            List<TbContent> contentList = (List<TbContent>)redisTemplate.boundHashOps(key).get(feild);
            return contentList;
        }else {
            //如果没有，再从数据库中查询相对应的数据，并加到Redis中
            List<TbContent> contents = contentMapper.getContentBannerInfo(categoryId);
            //将结果放入Redis,注意实体类必须序列化
            redisTemplate.boundHashOps(key).put(feild,contents);
            //设置有效时间。因为在springboot里村Redis数据时会将数据的key自动帮我们序列化。无法在可视化工具里删除
            redisTemplate.boundHashOps(key).expire(1, TimeUnit.MINUTES);
            return contents;
        }
    }
}
